package com.lancoo.answer.ui.answerSheet.vh;

import android.annotation.SuppressLint;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.entity.AnswerSheetChildBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.util.paperUtils.KindIndexStringUtils;
import com.lancoo.answer.widget.ChildFlowLayout;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by pengmi on 2022/2/22 9:06
 * Description:普通考试场景VH
 */
public class GeneralTestVH extends AnswerSheetBaseVH{

    private int enableShowAnswerScore;


    public static GeneralTestVH getGeneralTestVH(@NonNull ViewGroup parent,int enableShowAnswerScore){
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_item_type_answer_sheet, parent, false);
        return new GeneralTestVH(inflate,enableShowAnswerScore);
    }


    public GeneralTestVH(@NonNull View itemView,int enableShowAnswerScore) {
        super(itemView);
        this.enableShowAnswerScore=enableShowAnswerScore;
    }


    public void initData(int position,final AnswerSheetBean answerSheetBean){
        TextView tv_kindName=itemView.findViewById(R.id.tv_kind_name);
        int typeIndex = answerSheetBean.getTypeIndex();
        if(typeIndex!=0){
            typeIndex=typeIndex-1;
        }else{
            typeIndex=position;
        }

        int paperType = ConstantBean.Companion.getTaskControlBean().getPaperType();
        if(paperType==3){
            String name= answerSheetBean.getKindTypeName();
            tv_kindName.setText(Html.fromHtml(name).toString().trim());
            tv_kindName.setMaxLines(3);
            if(TextUtils.isEmpty(tv_kindName.getText())){
                tv_kindName.setVisibility(View.GONE);
            }else{
                tv_kindName.setVisibility(View.VISIBLE);
            }
        }else{
            String name= KindIndexStringUtils.getOrderIndex(typeIndex)+"、"+answerSheetBean.getKindTypeName();
            if(typeIndex<0){
                name=answerSheetBean.getKindTypeName();
            }
            if(enableShowAnswerScore==0){
                tv_kindName.setText(name);
            }else{
                SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
                SpannableString ss1=new SpannableString(name);
                ss1.setSpan(new ForegroundColorSpan(0xff666666),0,ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss1.setSpan(new AbsoluteSizeSpan(15,true),0,ss1.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                SpannableString ss2=new SpannableString("(");
                ss2.setSpan(new ForegroundColorSpan(0xff999999),0,ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss2.setSpan(new AbsoluteSizeSpan(14,true),0,ss2.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                SpannableString ss3=new SpannableString(""+answerSheetBean.getAnswerScore());
//            ss3.setSpan(new ForegroundColorSpan(answerSheetBean.isPassed()?0xff00b30d:0xffff0000),0,ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss3.setSpan(new ForegroundColorSpan(answerSheetBean.isPassed()?0xff0099ff:0xff0099ff),0,ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss3.setSpan(new AbsoluteSizeSpan(14,true),0,ss3.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                SpannableString ss4=new SpannableString("/");
                ss4.setSpan(new ForegroundColorSpan(0xff999999),0,ss4.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss4.setSpan(new AbsoluteSizeSpan(14,true),0,ss4.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                SpannableString ss5=new SpannableString(answerSheetBean.getTotalScore()+"分)");
                ss5.setSpan(new ForegroundColorSpan(0xff999999),0,ss5.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                ss5.setSpan(new AbsoluteSizeSpan(14,true),0,ss5.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss1);
                spannableStringBuilder.append(ss2);
                spannableStringBuilder.append(ss3);
                spannableStringBuilder.append(ss4);
                spannableStringBuilder.append(ss5);
                tv_kindName.setText(spannableStringBuilder);
            }

        }

        ChildFlowLayout childFlowLayout = itemView.findViewById(R.id.child_flow_layout);
        childFlowLayout.removeAllViews();
        for (AnswerSheetChildBean answerSheetChildBean:answerSheetBean.getAnswerSheetChildBeanList()){
            childFlowLayout.addView(addView(answerSheetChildBean));
        }
        childFlowLayout.setOnItemClickListener((position1, view) -> {
            if(System.currentTimeMillis()- ConstantBean.Companion.getOnClickSubTime()<200){
                return;
            }
            ConstantBean.Companion.setOnClickSubTime(System.currentTimeMillis());
            AnswerSheetChildBean bean = answerSheetBean.getAnswerSheetChildBeanList().get(position1);
            if(bean.getShowState()==5||bean.getShowState()==6){
                return;
            }
            onClickAnswerSheet(bean);
        });
    }

    public View addView(AnswerSheetChildBean bean){
        LayoutInflater layoutInflater = LayoutInflater.from(itemView.getContext());
        @SuppressLint("InflateParams") View inflate = layoutInflater .inflate(R.layout.ev_item_child_answer,null);
        TextView textView=inflate.findViewById(R.id.tv_child_index);
        textView.setText(bean.getSortIndex());
        ImageView imageView=inflate.findViewById(R.id.img_select_flag);
        imageView.setVisibility(bean.getIsSelected()==0?View.INVISIBLE:View.VISIBLE);
        textView.setTextColor(0xffffffff);
        inflate.findViewById(R.id.img_mark).setVisibility(bean.isMark()?View.VISIBLE:View.GONE);
        if(enableShowAnswerScore==1){
            textView.setTextSize(14);
        }
        switch (bean.getShowState()){
            case 1:
            case 4:
                if( ConstantBean.Companion.getTaskState()==TaskState.LOOKING){
                    textView.setBackgroundResource(R.mipmap.ev_icon_choice_blue);
                }else{
                    textView.setBackgroundResource(R.mipmap.ev_icon_test_choice_blue);
                }
                imageView.setImageResource(R.drawable.ev_vector_up_arror_09f);
                break;
            case 2:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_red);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_red);
                break;
            case 3:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_green);
                break;
            case 5:
                textView.setTextColor(0xffc3c3c3);
                textView.setBackgroundResource(R.mipmap.ev_icon_test_unenable_answer);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
                break;
            case 6:
                textView.setBackgroundResource(R.mipmap.ev_icon_test_unenable_answer_blue);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
                break;
            default:
                textView.setTextColor(0xff666666);
                textView.setBackgroundResource(R.mipmap.ev_icon_test_choice_default);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
        }
        return inflate;
    }
}
